package com.effectivsocial.core.jsf.extension;

import java.net.MalformedURLException;
import java.net.URL;

import javax.faces.context.ExternalContext;
import javax.faces.context.ExternalContextWrapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SimpleExternalContext extends ExternalContextWrapper{
	
	private static final Logger logger = LoggerFactory.getLogger(SimpleExternalContext.class);
	
	private ExternalContext wrapped;

    public SimpleExternalContext(ExternalContext wrapped) {
        this.wrapped = wrapped;
    }

    public URL getResource(String path) throws MalformedURLException {
    	logger.debug("Looking for " + path);
        URL url = Thread.currentThread().getContextClassLoader().getResource(path.substring(1));
        logger.debug("@@ URL" + url);
        if (url == null) {
            url = getWrapped().getResource(path);
            logger.debug("@@ URL " + url);
        }
        logger.debug("@@ Finally URL " + url);
        return url;
    }

    @Override
    public ExternalContext getWrapped() {
        return wrapped;
    }
}
